package vcp.client.GUI.mvc.view;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JLayer;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.text.JTextComponent;

import vcp.client.GUI.events.ValidationDescriptionEvent;
import vcp.client.GUI.mvc.controller.UIRootController;
import vcp.client.GUI.mvc.view.custom.CustomInputVerifier;
import vcp.client.GUI.mvc.view.effects.ValidationLayerUI;
import vcp.client.interfaces.listeners.IValidationListener;
import vcp.client.utility.Common;
import vcp.client.utility.Common.RegexStrings;
import vcp.client.utility.Common.UserNotification;
import vcp.client.utility.UtilityHelper;
import vcp.enums.PopUpMessageType;
import vcp.enums.Screen;
/**
 * <p> user interface check out screen class </p>
 * @author VCP team
 *
 */
public class UICheckoutView extends UIAbstractView
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JButton btnSubmit;
	private JButton btnCancel;
	private JFormattedTextField ftfCarID;

	private JLabel siteName;
	/**
	 * getter
	 * @return site name
	 */
	private JLabel getSiteName ()
	{
		if ( this.siteName == null )
		{
			this.siteName = new JLabel();
		}
		return this.siteName;
	}
	/**
	 * setter
	 * @param siteName
	 */
	public void setSiteName ( String siteName )
	{
		siteName = String.format("Site Name:%s", siteName);
		this.siteName = new JLabel(siteName);
		this.siteName.setFont(new Font("Lucida Grande", Font.BOLD, 12));
	}

	private IValidationListener validationListener;
	/**
	 * setter
	 * @param validationListener
	 */
	public void setValidationListener ( IValidationListener validationListener )
	{
		this.validationListener = validationListener;
	}

	private ValidationLayerUI carIDValidationLayerUI;
	/**
	 * constructor
	 */
	public UICheckoutView ()
	{
		super();

		this.resetView();
	}

	/****************************************************************************************************************************
	 *** UIAbstractView methods ***
	 ****************************************************************************************************************************/
	@Override
	public ArrayList <JComponent> getAdditionalToolbarComponents ()
	{
		this.getAdditionalComponents().clear();
		this.getAdditionalComponents().add(this.getSiteName());

		return this.getAdditionalComponents();
	}

	@Override
	public void clearView ()
	{
		this.ftfCarID.setText("");
	}

	@Override
	public void resetView ()
	{
		this.btnSubmit.setEnabled(false);
	}

	@Override
	protected void setupLayout ()
	{
		this.setLayout(new BorderLayout(0, 0));

		JPanel panel_5 = new JPanel();
		panel_5.setPreferredSize(new Dimension(100, 10));
		this.add(panel_5, BorderLayout.WEST);

		JPanel panel_6 = new JPanel();
		this.add(panel_6, BorderLayout.CENTER);
		panel_6.setLayout(new GridLayout(2, 0, 0, 0));

		JPanel panel = new JPanel();
		panel_6.add(panel);
		panel.setLayout(new GridLayout(5, 1, 0, 0));

		JPanel panel_3 = new JPanel();
		panel.add(panel_3);

		JPanel panel_4 = new JPanel();
		panel.add(panel_4);

		JPanel panel_2 = new JPanel();
		panel.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 3, 0, 0));

		JLabel lblNewLabel = new JLabel(Common.LBL_CAR_ID);
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		panel_2.add(lblNewLabel);
		lblNewLabel.setFont(new Font("Lucida Grande", Font.BOLD, 32));

		this.ftfCarID = new JFormattedTextField();
		this.ftfCarID.setToolTipText("Format:#######");
		JLayer <JTextComponent> carIDLayer = new JLayer <JTextComponent>(this.ftfCarID);
		this.carIDValidationLayerUI = new ValidationLayerUI();
		carIDLayer.setUI(this.carIDValidationLayerUI);
		panel_2.add(carIDLayer);
		this.ftfCarID.setFont(new Font("Lucida Grande", Font.PLAIN, 32));
		this.ftfCarID.setPreferredSize(new Dimension(200, 50));
		this.ftfCarID.setHorizontalAlignment(SwingConstants.LEFT);

		JPanel panel_1 = new JPanel();
		panel_6.add(panel_1);
		panel_1.setLayout(new GridLayout(4, 0, 0, 0));

		JPanel panel_8 = new JPanel();
		panel_1.add(panel_8);
		panel_8.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel panel_9 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_9.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		panel_8.add(panel_9);

		this.btnCancel = new JButton(Common.BTN_CANCEL);
		panel_9.add(this.btnCancel);
		this.btnCancel.setPreferredSize(new Dimension(175, 75));
		this.btnCancel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.btnCancel.setFocusable(false);
		this.btnCancel.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		this.btnCancel.setName(Common.BTN_CANCEL);

		JPanel panel_10 = new JPanel();
		panel_8.add(panel_10);

		JPanel panel_11 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_11.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		panel_8.add(panel_11);

		this.btnSubmit = new JButton(Common.BTN_SUBMIT);
		panel_11.add(this.btnSubmit);
		this.btnSubmit.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.btnSubmit.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		this.btnSubmit.setName(Common.BTN_SUBMIT);
		this.btnSubmit.setPreferredSize(new Dimension(175, 75));

		JPanel panel_7 = new JPanel();
		panel_7.setPreferredSize(new Dimension(100, 10));
		this.add(panel_7, BorderLayout.EAST);
	}

	@Override
	protected void setupListeners ()
	{
		this.btnSubmit.addActionListener(this);
		this.btnCancel.addActionListener(this);

		this.ftfCarID.addKeyListener(new KeyListener()
		{

			@Override
			public void keyTyped ( KeyEvent e )
			{
			}

			@Override
			public void keyReleased ( KeyEvent e )
			{
				String fieldCarIDText = UICheckoutView.this.ftfCarID.getText();

				boolean result = UtilityHelper.getUtilityHelperInstance().isParamsNotNull(fieldCarIDText);
				UICheckoutView.this.btnSubmit.setEnabled(result);
			}

			@Override
			public void keyPressed ( KeyEvent e )
			{
			}
		});

		Pattern carIDPattern = UIRootController.DEBUG
				? Pattern.compile(RegexStrings.ACCEPT_ALL)
						: Pattern.compile(RegexStrings.CAR_ID_7_DIGITS);
				this.ftfCarID.setInputVerifier(new CustomInputVerifier(this.carIDValidationLayerUI, carIDPattern));
	}

	@Override
	protected void setupButtonsIcons ()
	{
		int iconTextGap = 20;

		ImageIcon icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_SUBMIT, 45, 45);
		if ( icon != null )
		{
			this.btnSubmit.setIcon(icon);
			this.btnSubmit.setIconTextGap(iconTextGap);
		}

		icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_CANCEL, 45, 45);
		if ( icon != null )
		{
			this.btnCancel.setIcon(icon);
			this.btnCancel.setIconTextGap(iconTextGap);
		}
	}

	/****************************************************************************************************************************
	 *** ActionListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		JButton checkOutViewButton = (JButton) actionEvent.getSource();
		switch ( checkOutViewButton.getName() )
		{
			case Common.BTN_SUBMIT:
				if ( this.validationListener != null )
				{
					if ( this.ftfCarID.getInputVerifier().verify(this.ftfCarID) )
					{
						Integer carID = Integer.parseInt(this.ftfCarID.getText());
						this.validationListener.onValidationRequest(new ValidationDescriptionEvent(this, carID));
					}
					else
					{
						String infoMessage = String.format(UserNotification.NOT_CORRECT_FORMAT_FOR__VALUE__, this.ftfCarID.getToolTipText());
						this.controllerDelegate.showPopUpMessage(infoMessage, PopUpMessageType.INFORMATION);
					}
				}
				break;
			case Common.BTN_CANCEL:
				this.controllerDelegate.onButtonPress(Screen.ONSITE);
				break;
		}
	}
}
